package org.dromara.system.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.bo.SysBuildingUnitBo;
import org.dromara.system.domain.vo.SysBuildingUnitVo;
import org.dromara.system.service.ISysBuildingUnitService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 房产单元管理
 *
 * @author LSL
 * @date 2024-05-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/buildingUnit")
public class SysBuildingUnitController extends BaseController {

    private final ISysBuildingUnitService sysBuildingUnitService;

    /**
     * 查询房产单元管理列表
     */
    @SaCheckPermission("system:buildingUnit:list")
    @GetMapping("/list")
    public TableDataInfo<SysBuildingUnitVo> list(SysBuildingUnitBo bo, PageQuery pageQuery) {
        return sysBuildingUnitService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出房产单元管理列表
     */
    @SaCheckPermission("system:buildingUnit:export")
    @Log(title = "房产单元管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysBuildingUnitBo bo, HttpServletResponse response) {
        List<SysBuildingUnitVo> list = sysBuildingUnitService.queryList(bo);
        ExcelUtil.exportExcel(list, "房产单元管理", SysBuildingUnitVo.class, response);
    }

    /**
     * 获取房产单元管理详细信息
     *
     * @param unitId 主键
     */
    @SaCheckPermission("system:buildingUnit:query")
    @GetMapping("/{unitId}")
    public R<SysBuildingUnitVo> getInfo(@NotNull(message = "主键不能为空")
                                            @PathVariable Long unitId) {
        return R.ok(sysBuildingUnitService.queryById(unitId));
    }

    /**
     * 新增房产单元管理
     */
    @SaCheckPermission("system:buildingUnit:add")
    @Log(title = "房产单元管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysBuildingUnitBo bo) {
        return toAjax(sysBuildingUnitService.insertByBo(bo));
    }

    /**
     * 修改房产单元管理
     */
    @SaCheckPermission("system:buildingUnit:edit")
    @Log(title = "房产单元管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysBuildingUnitBo bo) {
        return toAjax(sysBuildingUnitService.updateByBo(bo));
    }

    /**
     * 删除房产单元管理
     *
     * @param unitIds 主键串
     */
    @SaCheckPermission("system:buildingUnit:remove")
    @Log(title = "房产单元管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{unitIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] unitIds) {
        return toAjax(sysBuildingUnitService.deleteWithValidByIds(List.of(unitIds), true));
    }
}
